/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Text;
using NUnit.Framework;
using OpenServers.Net.Common;
using OpenServers.Net.Common.Listener;
using OpenServers.Net.Common.Socket;
using OpenServers.Net.Common.Utils;
using OpenServers.Net.HttpServer.Request;
using OpenServers.Net.HttpServer.Response;
using OpenServers.Net.HttpServer.State;

namespace OpenServers.Net.HttpServer {
    [TestFixture]
    public class HttpProcessorTest : BaseNMockTest {
        private IHttpServer _httpServer;
        private PrivateObject<HttpProcessor> _httpProcessor;
        private HttpState _state;
        private IListener _listener;
        private ISocket _clientSocket;

        [SetUp]
        public override void SetUp() {
            base.SetUp();

            _httpServer = Mocks.NewMock<IHttpServer>();
            _listener = Mocks.NewMock<IListener>();
            _clientSocket = Mocks.NewMock<ISocket>();
            _httpProcessor = new PrivateObject<HttpProcessor>(new HttpProcessor(_httpServer));
            _state = new HttpState(_listener, _clientSocket);
            _state.Response = new HttpResponse();
        }

        [Test]
        public void TestDataReceivedLeadingNewLines() {
            string str = "\r\n\r\n\r\nGET /test.html HTTP/1.1\r\n\r\n";
            byte[] buffer = Encoding.ASCII.GetBytes(str);
            _httpProcessor.WrappedObject.DataReceived(_state, buffer, buffer.Length);

            Assert.AreEqual("GET", _state.Request.Verb);
            Assert.AreEqual("/test.html", _state.Request.Url.ToString());
            Assert.AreEqual(1.1, _state.Request.HttpVersion);
        }

        [Test]
        public void TestParseHeadersWithHeaderLines() {
            string str = "GET /test.html HTTP/1.1\nX-HeaderField: value\n\n";
            _httpProcessor.ExecuteMethod("ParseHeaders", _state, str);

            Assert.IsNotNull(_state.Request);
            Assert.IsTrue(_state.Request.Headers.ContainsKey("X-HeaderField"));
            Assert.AreEqual("value", _state.Request.Headers["X-HeaderField"]);
        }

        [Test]
        public void TestAddHeaderNameValueDuplicateNames() {
            _state.Request = new HttpRequest("GET", new Url("/"), 1.1);

            _httpProcessor.ExecuteMethod("AddHeaderNameValue", _state, "X-HeaderField", "a");
            _httpProcessor.ExecuteMethod("AddHeaderNameValue", _state, "X-HeaderField", "b");

            Assert.IsTrue(_state.Request.Headers.ContainsKey("X-HeaderField"));
            Assert.AreEqual("a, b", _state.Request.Headers["X-HeaderField"]);
        }

        [Test]
        public void TestParseHeaderLineContinuation() {
            _state.Request = new HttpRequest("GET", new Url("/"), 1.1);

            _httpProcessor.ExecuteMethod("ParseHeaderLine", _state, "X-HeaderField: abcd", null);
            _httpProcessor.ExecuteMethod("ParseHeaderLine", _state, " efghij", "X-HeaderField");

            Assert.IsTrue(_state.Request.Headers.ContainsKey("X-HeaderField"));
            Assert.AreEqual("abcdefghij", _state.Request.Headers["X-HeaderField"]);
        }

        [Test]
        [ExpectedException(ExceptionType = typeof(ApplicationException))]
        public void TestParseHeaderLineContinuationNoPreviousHeader() {
            _state.Request = new HttpRequest("GET", new Url("/"), 1.1);

            _httpProcessor.ExecuteMethod("ParseHeaderLine", _state, " efghij", null);
        }

        [Test]
        [ExpectedException(ExceptionType = typeof(ApplicationException))]
        public void TestParseHeaderLineInvalid() {
            _state.Request = new HttpRequest("GET", new Url("/"), 1.1);

            _httpProcessor.ExecuteMethod("ParseHeaderLine", _state, "X-HeaderField", null);
        }
    }
}
